package appapi.controller.oider;

import appapi.Vo.AddEquipTypeItemVo;
import appapi.Vo.EditEquipTypeItemVo;
import appapi.common.ResponseBean;
import appapi.entity.basic.EquipTypeItemEntity;
import appapi.service.EquipTypeItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/30 12:53
 */
@Api(tags = "设备类型自检项目")
@RestController
@RequestMapping("/api/equipTypeItem")
@Validated
public class EquipTypeItemController  {
    private static final Logger logger = LoggerFactory.getLogger(EquipTypeItemController.class);
        @Autowired
        private EquipTypeItemService equipTypeItemService;
        /**
         * 列表
         */
        @GetMapping("/getList")
        @ApiOperation(value = "获取列表")
        public ResponseBean<EquipTypeItemEntity> list(@Valid Long equipTypeId) {
            try {
                List<EquipTypeItemEntity> dataList = equipTypeItemService.list(equipTypeId);
                return ResponseBean.success(dataList);
            } catch (Exception e) {
                logger.error("获取列表异常", e);
                return ResponseBean.fail("获取列表异常");
            }
        }

        /**
         * 信息
         */
        @GetMapping("/getDetail")
        @ApiOperation(value = "获取详情")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "equipItemId", dataType = "long", value = "项目Id",required = true)
        })
        public ResponseBean detail(@Valid Long equipItemId) {
            try {
                if (equipItemId == null) {
                    return ResponseBean.fail("ID不能为空");
                }
                EquipTypeItemEntity entity = equipTypeItemService.detail(equipItemId);
                if (entity != null) {
                    return ResponseBean.success(entity);
                } else {
                    return ResponseBean.fail("获取详情失败");
                }
            } catch (Exception e) {
                logger.error("获取详情异常", e);
                return ResponseBean.fail("获取详情异常");
            }
        }

        /**
         * 新增
         */
        @ApiOperation(value = "新增信息")
        @PostMapping(value = "/addInfo")

        public ResponseBean addInfo(@Valid @RequestBody  AddEquipTypeItemVo vo) {
//            try {
                EquipTypeItemEntity entity = vo.vo2Entity();
                entity.setCreateTime(LocalDateTime.now());
                entity.setUpdateTime(LocalDateTime.now());
                int count = equipTypeItemService.count(entity);
                if (count >= 1) {
                    return ResponseBean.fail("名称已存在，不能重复添加");
                }
                int iRet = equipTypeItemService.insert(entity);
                if (iRet > 0) {
                    return ResponseBean.success("保存成功");
                } else {
                    return ResponseBean.fail("保存失败");
                }
//            } catch (Exception e) {
//                logger.error("新增异常", e);
//                return ResponseBean.fail("新增异常");
//            }
        }

        /**
         * 修改
         */
        @ApiOperation(value = "修改信息")
        @PostMapping(value = "/editInfo")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "formData", name = "EditEquipTypeItemVo", dataType = "object", value = "类别参数")
        })
        public ResponseBean update(@Valid @RequestBody EditEquipTypeItemVo vo) {
            try {
                EquipTypeItemEntity entity = vo.vo2Entity();
                entity.setUpdateTime(LocalDateTime.now());
                int count = equipTypeItemService.count(entity);
                if (count >= 1) {
                    return ResponseBean.fail("名称已存在，不能重复修改");
                }
                int iRet = equipTypeItemService.update(entity);
                if (iRet > 0) {
                    return ResponseBean.success("更新成功");
                } else {
                    return ResponseBean.fail("更新失败");
                }
            } catch (Exception e) {
                logger.error("更新异常", e);
                return ResponseBean.fail("更新异常");
            }
        }

        /**
         * 删除
         */
        @ApiOperation(value = "删除信息")
        @PostMapping(value = "/deleteInfo")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "equipItemId", dataType = "long", value = "项目Id",required = true)
        })
        public ResponseBean delete(Long equipItemId) {
            try {
                if (equipItemId == null) {
                    return ResponseBean.fail("ID不能为空");
                }
                int iRet = equipTypeItemService.delete(equipItemId);
                if (iRet == -1) {
                    return ResponseBean.fail("存在引用不能删除");
                }
                if (iRet == -2) {
                    return ResponseBean.fail("对象不存在");
                }
                else if (iRet > 0) {
                    return ResponseBean.success("删除成功");
                } else {
                    return ResponseBean.fail("删除失败");
                }
            } catch (Exception e) {
                logger.error("删除异常", e);
                return ResponseBean.fail("删除异常");
            }
        }
}

